package com.whale.util;

import java.util.concurrent.Callable;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.os.AsyncTask;
import android.util.Log;

public class UiUtil {

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dp2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    public static <T> void doAsync(final Context pContext, final CharSequence pMessage, final Callable<T> pCallable,
                                   final Callback<T> pCallback) {
        UiUtil.doAsync(pContext, pMessage, true, pCallable, pCallback, null, false);
    }

    public static <T> void doAsync(final Context pContext, final CharSequence pMessage, final boolean bShowPD,
                                   final Callable<T> pCallable, final Callback<T> pCallback,
                                   final Callback<Exception> pExceptionCallback, final boolean pCancelable) {
        new AsyncTask<Void, Void, T>() {
            private ProgressDialog progressDialog;
            private Exception      mException = null;
            @Override
            public void onPreExecute() {
                if (bShowPD) {
                    progressDialog = new ProgressDialog(pContext);
                    progressDialog.setMessage(pMessage);
                    progressDialog.show();
                    if (pCancelable) {
                        this.progressDialog.setOnCancelListener(new OnCancelListener() {

                            @Override
                            public void onCancel(final DialogInterface pDialogInterface) {
                                pExceptionCallback.onCallback(new CancelledException());
                                pDialogInterface.dismiss();
                            }
                        });
                    }
                }
                super.onPreExecute();
            }

            @Override
            public T doInBackground(final Void... params) {
                try {
                    return pCallable.call();
                } catch (final Exception e) {
                    this.mException = e;
                }
                return null;
            }

            @Override
            public void onPostExecute(final T result) {
                if (bShowPD) {
                    try {
                        this.progressDialog.cancel();
                    } catch (final Exception e) {
                        Log.e("Error", e.toString());
                    }
                }

                if (this.isCancelled()) {
                    this.mException = new CancelledException();
                }

                if (this.mException == null) {
                    pCallback.onCallback(result);
                } else {
                    if (pExceptionCallback == null) {
                        if (this.mException != null) Log.e("Error", this.mException.toString());
                    } else {
                        pExceptionCallback.onCallback(this.mException);
                    }
                }
                super.onPostExecute(result);
            }
        }.execute((Void[]) null);
    }

}
